//==========================================================================
// Copyright (c) 2000-2006,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#ifndef __FRAMEBUFFER_H__
#define __FRAMEBUFFER_H__

#include <display.h>
typedef void (*V2VFUNC)(void);

#define UNTRANSRGBFBPIXELBYTES   3

typedef struct {
    int Width;
    int Height;
    int PosX;
    int PosY;
//    WORD *OverlapFb;
}overlapFormat;

#define WORK_BUFFER_BYTES 4

typedef struct {
    int nMobileWidth;
    int nMobileHeight;

    int nLCDPosX;
    int nLCDPosY;

    int nLCDWidth;
    int nLCDHeight;

    //Dual Screen support
    BOOL    bDoubleScreen;       //Whether support two screen
    BOOL    bRgb24ToLut4;        //Whether Blit RGB24 to LUT4
    float   fMajorShrinkRateX;   //The shrink rate of the Major screen
    float   fMajorShrinkRateY;   //The shrink rate of the Major screen
    float   fMinorShrinkRateX;   //The shrink rate of the Minor screen
    float   fMinorShrinkRateY;   //The shrink rate of the Minor screen
    int     nMajorLCDPosX;
    int     nMajorLCDPosY;
    int     nMajorLCDWidth;
    int     nMajorLCDHeight;
    int     nMinorLCDPosX;
    int     nMinorLCDPosY;
    int     nMinorLCDWidth;
    int     nMinorLCDHeight;
    int     nTouchResponseLCD;      //Which LCD to response for mouse touch event

    int nLCDColorBits;
    int nTransparencyKey;

    // Usually bLCDTransSupport == bOverlay2Support
    BOOL bIsOverlap;//When overlay is setted, there must overlay1 or overlay2 be setted
    BOOL bIsSetOverlay1;//When bIsSetOverlay1 is TURE, bIsOverlay must be TURE
    BOOL bIsSetOverlay2;//When bIsSetOverlay2 is TURE, bIsOverlay must be TURE
    BOOL bIsUnderlay;//TURE : Underlay

    BOOL bShowLCDOnly;

    ColorFormat BasePlaneColorFormat;
    int nBasePlanePixelBits;
    int nBasePlanePixelBytes;
    void *pBasePlaneFb; // framebuffer address of base plane
    UINT nBasePlaneSize; // framebuffer size (bytes)

    void *pWorkBuffer;
    UINT nWorkBufferSize;

    OverlayFormat overlay1;
    int nOverlay1PixelBytes;
    void *pOverlay1Fb; // framebuffer address of Overlay1 plane
    UINT nOverlay1Size; // framebuffer size (bytes)

    OverlayFormat overlay2;
    void *pOverlay2Fb; // framebuffer address of YCbCr Overlay2
    UINT nOverlay2Size; // framebuffer size (bytes)

    BYTE *pTransArray; //Transparence Array used to record the baseplane transparence bits
    UINT nTransArraySize; // Transparence Array size

    overlapFormat overlapArea;//The Area is overlaped by three layer

    V2VFUNC pBaseplaneBufferCopy;
    V2VFUNC pOverlay2BufferCopy;
    V2VFUNC pOverlay1BufferCopy;
    V2VFUNC OverlapAreaOperation;//Some special operation of the Overlap area

    BOOL bRender;
    BOOL bAutoRender;

    //func for GSensor

    V2VFUNC UpdateRect;
    V2VFUNC WorkBuffer2Surface;

} VideoContext;

EXTERN_C void * Video_InitBasePlaneFramebuffer();
EXTERN_C BOOL Video_SetOverlay(OverlayFormat *pOverlayFormat);
EXTERN_C void Video_DisableOverlay(int layer);
EXTERN_C void Video_Render();
EXTERN_C void Video_AutoRender();
EXTERN_C void Video_SetTransLevel(BYTE level);
EXTERN_C BOOL Video_InitVideo();


// for power management
typedef struct {
    V2VFUNC PowerOffCopy;
    V2VFUNC DarkenCopy;
    V2VFUNC NormalCopy;
}BufferCopy;

// color format exchange

// RGB32 -> others
#define ColorRGB888ToRGB565(color) (WORD)(((color&0x00F80000)>>8) | \
                                ((color&0x0000FC00)>>5) | \
                                ((color&0x000000F8)>>3))

#define ColorRGB888ToRGBT555(color) (WORD)(((color&0x00F80000)>>9) | \
                                ((color&0x0000F800)>>6) | \
                                ((color&0x000000F8)>>3))

#define ColorRGB888ToRGBT666(color) (DWORD)(((color&0x00FC0000)>>6) | \
                                ((color&0x0000FC00)>> 4) | \
                                ((color&0x000000FC)>>2))

// RGB565 -> others
#define ColorRGB565ToRGB888(color) (DWORD)((((DWORD)color&0xF800)<<8) | \
                                (((DWORD)color&0x07E0)<< 5) | \
                                (((DWORD)color&0x001F)<<3))

#define ColorRGB565ToRGBT555(color) (WORD)(((color&0xFFE0)>>1) | \
                                (color&0x001F))

#define ColorRGB565ToRGBT666(color)  (DWORD)((((DWORD)color&0xF800)<<2) | \
                                (((DWORD)color&0x07FF)<<1))

// RGB555 -> others
#define ColorRGBT555ToRGB888(color) (DWORD)((((DWORD)color&0x7C00)<<9) | \
                                (((DWORD)color&0x03E0)<<6) | \
                                (((DWORD)color&0x001F)<<3))

#define ColorRGBT555ToRGB565(color) (WORD)((((WORD)color&0xFFE0) << 1) | \
                                ((WORD)color & 0x001F))

#define ColorRGBT555ToRGBT666(color) (DWORD)((((DWORD)color&0x7C00)<<3) | \
                                (((DWORD)color&0x03E0)<<2) | \
                                (((DWORD)color&0x001F)<<1))

// RGB666 -> others
#define ColorRGBT666ToRGB888(color) (DWORD)(((color&0x03F000) << 6) | \
                                ((color&0x0FC0)<<4) | \
                                ((color&0x3F)<<2))

#define ColorRGBT666ToRGB565(color) (WORD)(((color&0x03E000) >> 2) | \
                                ((color&0x0FFF)>>1))

#define ColorRGBT666ToRGBT555(color) (WORD)(((color&0x03E000) >> 3) | \
                                ((color&0x0F80)>>2) | \
                                ((color&0x3E)>>1))

#endif // __FRAMEBUFFER_H__
